/*
 * Copyright (C) 2022  即时通讯网(52im.net) & Jack Jiang.
 * The MobileIMSDK_TCP (MobileIMSDK v6.1 TCP版) Project.
 * All rights reserved.
 *
 * > Github地址：https://github.com/JackJiang2011/MobileIMSDK
 * > 文档地址：  http://www.52im.net/forum-89-1.html
 * > 技术社区：  http://www.52im.net/
 * > 技术交流群：215477170 (http://www.52im.net/topic-qqgroup.html)
 * > 作者公众号：“即时通讯技术圈】”，欢迎关注！
 * > 联系作者：  http://www.52im.net/thread-2792-1-1.html
 *
 * "即时通讯网(52im.net) - 即时通讯开发者社区!" 推荐开源工程。
 *
 * ClientCoreSDK.java at 2022-7-16 17:22:43, code by Jack Jiang.
 */
package com.dx.mobileimsdk.client;

import com.dx.mobileimsdk.client.core.*;
import com.dx.mobileimsdk.client.event.ChatBaseEvent;
import com.dx.mobileimsdk.client.event.ChatMessageEvent;
import com.dx.mobileimsdk.client.event.MessageQoSEvent;
import net.x52im.mobileimsdk.server.protocal.c.PLoginInfo;

public class ClientCoreSDK {

    private final static String TAG = ClientCoreSDK.class.getSimpleName();

    public static boolean DEBUG = true;
    public static boolean autoReLogin = true;
    private static ClientCoreSDK instance = null;

    private boolean _init = false;
    private boolean connectedToServer = true;
    private boolean loginHasInit = false;
    private PLoginInfo currentLoginInfo = null;

    private ChatBaseEvent chatBaseEvent = null;
    private ChatMessageEvent chatMessageEvent = null;
    private MessageQoSEvent messageQoSEvent = null;

    private ClientCoreSDK() {

    }

    public static ClientCoreSDK getInstance() {
        if (instance == null) {
            synchronized (ClientCoreSDK.class) {
                if (instance == null) {
                    instance = new ClientCoreSDK();
                }
            }
        }
        return instance;
    }

    public void init() {
        if (!_init) {
            _init = true;
        }
    }

    public void release() {
        this.setConnectedToServer(false);

        LocalSocketProvider.getInstance().closeLocalSocket();
        AutoReLoginDaemon.getInstance().stop();
        QoS4SendDaemon.getInstance().stop();
        KeepAliveDaemon.getInstance().stop();

        QoS4ReciveDaemon.getInstance().stop();

        QoS4SendDaemon.getInstance().clear();
        QoS4ReciveDaemon.getInstance().clear();

        _init = false;
        this.setLoginHasInit(false);
//		this.setConnectedToServer(false);
    }

    public PLoginInfo getCurrentLoginInfo() {
        return this.currentLoginInfo;
    }

    public void setCurrentLoginInfo(PLoginInfo currentLoginInfo) {
        this.currentLoginInfo = currentLoginInfo;
    }

    public void saveFirstLoginTime(long firstLoginTime) {
        if (this.currentLoginInfo != null)
            this.currentLoginInfo.setFirstLoginTime(firstLoginTime);
    }

    @Deprecated
    public String getCurrentLoginUserId() {
        return this.currentLoginInfo.getLoginUserId();
    }

    @Deprecated
    public String getCurrentLoginToken() {
        return this.currentLoginInfo.getLoginToken();
    }

    @Deprecated
    public String getCurrentLoginExtra() {
        return this.currentLoginInfo.getExtra();
    }

    public boolean isLoginHasInit() {
        return loginHasInit;
    }

    public ClientCoreSDK setLoginHasInit(boolean loginHasInit) {
        this.loginHasInit = loginHasInit;
        return this;
    }

    public boolean isConnectedToServer() {
        return connectedToServer;
    }

    public void setConnectedToServer(boolean connectedToServer) {
        this.connectedToServer = connectedToServer;
    }

    public boolean isInitialed() {
        return this._init;
    }

    public ChatBaseEvent getChatBaseEvent() {
        return chatBaseEvent;
    }

    public void setChatBaseEvent(ChatBaseEvent chatBaseEvent) {
        this.chatBaseEvent = chatBaseEvent;
    }

    public ChatMessageEvent getChatMessageEvent() {
        return chatMessageEvent;
    }

    public void setChatMessageEvent(ChatMessageEvent chatMessageEvent) {
        this.chatMessageEvent = chatMessageEvent;
    }

    public MessageQoSEvent getMessageQoSEvent() {
        return messageQoSEvent;
    }

    public void setMessageQoSEvent(MessageQoSEvent messageQoSEvent) {
        this.messageQoSEvent = messageQoSEvent;
    }
}
